package com.kaluli.project.repository.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.kaluli.project.domain.entity.KllOrderDataVo;
import com.kaluli.project.domain.entity.KllUserDataVo;
import com.kaluli.project.domain.entity.KllHupuUserVo;
import com.kaluli.project.domain.repository.KllDataRepository;
import com.kaluli.project.domain.repository.KllHupuUserRepository;
import com.kaluli.project.domain.repository.KllUserDataRepository;
import com.kaluli.system.repository.AbstractRepositorySupport;

@Repository
public class KllHupuUserRepositoryImpl extends AbstractRepositorySupport<KllHupuUserVo> implements KllHupuUserRepository{

	@Override
	protected Class<KllHupuUserVo> getEntityClass() {
		// TODO Auto-generated method stub
		return KllHupuUserVo.class;
	}

	
	
	/**
	 * 当天Hupu下单用户
	 * @return
	 */
	public List<KllHupuUserVo> findHupuUserByToday(String type) {
		StringBuffer sql = new StringBuffer(" SELECT m.id,m.pay_time as payTime,m.hupu_uid as hupuUid ");
		sql.append(" FROM kll_user u,kll_main_order m  ");
		sql.append(" WHERE u.`user_id` = m.`hupu_uid`  AND u.source=0  ");
		if(type.equals("week")){
			sql.append(" AND DATEDIFF(NOW(),m.`order_time`)>=0 and DATEDIFF(NOW(),m.`order_time`)<7 ");
		}else if(type.equals("today")){
			sql.append(" AND DATEDIFF(m.`order_time`,NOW())=0  ");
		}
		
		if(type.equals("pay")){
			sql.append(" AND m.`pay_time` IS NOT NULL ");
		}
		
		List<KllHupuUserVo> list = excuteSql(sql.toString());
		if(list != null && list.size()>0){
			if(list.get(0) == null){
				return null;
			}
		}
		return list;
	}
	
	
	/**
	 * 每周统计用户
	 * @return
	 */
	public List<KllHupuUserVo> findUserByWeek(String source) {
		StringBuffer sql = new StringBuffer(" SELECT m.id,m.pay_time as payTime,m.hupu_uid as hupuUid ");
		sql.append(" FROM kll_user u,kll_main_order m  ");
		sql.append(" WHERE u.`user_id` = m.`hupu_uid`  AND u.source=0  ");
		sql.append(" AND DATEDIFF(NOW(),m.`order_time`)>=0 and DATEDIFF(NOW(),m.`order_time`)<7 ");
		if(source.equals("PC")){
			sql.append(" and u.source = 1 ");
		}else if(source.equals("M")){
			sql.append(" and u.source = 2 ");
		}
		List<KllHupuUserVo> list = excuteSql(sql.toString());
		if(list != null && list.size()>0){
			if(list.get(0) == null){
				return null;
			}
		}
		return list;
	}
	
	
	public List<KllHupuUserVo> findOrderByUid(String uid) {
		StringBuffer sql = new StringBuffer(" SELECT m.id ,m.pay_time as payTime,m.hupu_uid as hupuUid ");
		sql.append(" FROM kll_main_order m  ");
		sql.append(" WHERE 1=1 ");
		sql.append(" AND m.`pay_time` IS NOT NULL ");
		sql.append(" and m.hupu_uid = "+uid);
		
		
		List<KllHupuUserVo> list = excuteSql(sql.toString());
		if(list != null && list.size()>0){
			if(list.get(0) == null){
				return null;
			}
		}
		return list;
	}
	
	
	

	

}
